import { ordinaryReview, update, queryListByVolumeForRegularReview } from '@/api/basicInfo'

const state = {
  topData: [], // 商品详情
  lowerData: [], // 订单详情
  isDisabled: false, // 波次是否禁用
  letData: [],
  barCode: '', // 商品条码
  barNum: '',
  complete: false,
  greater: false,
  none: false,
  num: null,
  sn: '',
  parcelType: '',
  printTest: '#printTest',
  modelPrintFlag: false,
  goodsName: '',
  cb: () => {},
  printData: [],
  form: {
    wavePickingCode: '',
    orderTag: '',
    shipmentName: '',
    carrierCode: '',
    deliveryCode: '',
    shopNick: '',
    deliveryItemCode: '',
    outboundCode: ''
  },
  msg: '',
  selectData: [],
  orderOccupy: true, // 订单占用
  cancelOrderPrompt: '' // 复核提示
}
const mutations = {
  SET_CANCEL_ORDER_PROMPT: (state, val) => {
    state.cancelOrderPrompt = val
  },
  SET_ORDER_OCCUPY: (state, val) => {
    state.orderOccupy = val
  },
  SET_SELETELIST: (state, selectData) => {
    state.selectData = selectData
  },
  SET_MSG: (state, msg) => {
    state.msg = msg
  },
  SET_PRCELTYPE: (state, parcelType) => {
    state.parcelType = parcelType
  },
  SET_PRINTDATA: (state, printData) => {
    state.printData = printData
  },
  SET_COMPLETE: (state, complete) => {
    state.complete = complete
  },
  SET_NONE: (state, none) => {
    state.none = none
  },
  SET_GREATER: (state, greater) => {
    state.greater = greater
  },
  SET_CELLBACK: (state, cb) => {
    state.cb = cb
  },
  SET_FLAG: (state, modelPrintFlag) => {
    state.modelPrintFlag = modelPrintFlag
  },
  SET_TIODATA: (state, topData) => {
    state.topData = topData
  },
  SET_LOWERDATA: (state, lowerData) => {
    state.lowerData = lowerData
  },
  SET_FORM: (state, form) => {
    state.form = form
    state.isDisabled = true
  },
  SET_ISDISABLED: (state, isDisabled) => {
    state.isDisabled = isDisabled
  },
  SET_LETDATA: (state, letData) => {
    state.letData = letData
  },
  SET_BARCODE: (state, barCode) => {
    state.barCode = barCode
  },
  SET_BARNUM: (state, barNum) => {
    state.barNum = barNum
  },
  SET_NUM: (state, num) => {
    state.num = num
  },
  SET_SN: (state, sn) => {
    state.sn = sn
  },
  SET_GOODSNAME: (state, goodsName) => {
    state.goodsName = goodsName
  }
}

const actions = {
  // 商品条码
  barCode({ commit }, val) {
    const { form } = state
    if (!form.wavePickingCode) return 1
    return true
  },
  // 波次号请求
  requestWaveNumber({ commit }, val) {
    const { cb } = state
    const pramas = {
      code: val,
      goodsBarCode: state.barCode,
      ...(state.orderOccupy && { deliveryCode: state.form.code })
    }
    commit('SET_ORDER_OCCUPY', true)
    // 请求接口
    return ordinaryReview(pramas).then(res => {
      commit('SET_CANCEL_ORDER_PROMPT', res.data.outbound.cancelOrderPrompt)
      if (res.data.status === 0) {
        const params = res.data.deliverieList.map(v => {
          return { volume: v.volume, deliveryCode: v.code }
        })
        queryListByVolumeForRegularReview(params).then(req => {
          const deliverieListData = res.data.deliverieList.map((a, i) => {
            if (a.code === req.data[i].deliveryCode) {
              if (a.packingMaterialName) return { ...a, packingMaterialList: req.data[i].packingMaterialList }
              return {
                ...a,
                packingMaterialList: req.data[i].packingMaterialList,
                packingMaterialName: req.data[i].packingMaterialList[0].name,
                packingMaterialCode: req.data[i].packingMaterialList[0].code
              }
            }
          })
          commit('SET_SELETELIST', req.data)
          commit('SET_COMPLETE', false)
          commit('SET_GREATER', false)
          commit('SET_NONE', false)
          if (state.barCode) {
            const obj = res.data.goodsList.find(v => state.barCode.toUpperCase() === v.goodsBarCode.toUpperCase())
            if (!obj) return false
            const goodsName = obj.goodsName
            commit('SET_GOODSNAME', goodsName)
          }
          commit('SET_COMPLETE', false)
          // 设置foem
          const form = {
            wavePickingCode: res.data.outbound.wavePickingCode,
            orderTag: translateStatus(res.data.outbound.orderTag),
            shipmentName: res.data.outbound.shipmentName,
            carrierCode: res.data.outbound.carrierCode,
            carrierName: res.data.outbound.carrierName,
            deliveryCode: '',
            code: res.data.deliverieList[0].code,
            shopNick: res.data.deliverieList[0].shopNick,
            deliveryItemCode: res.data.goodsList[0].code,
            outboundCode: res.data.outbound.code
          }
          const arr = [res.data.outbound]
          commit('SET_FORM', form)
          commit('SET_TIODATA', res.data.goodsList)
          commit('SET_LOWERDATA', deliverieListData)
          commit('SET_LETDATA', arr)
          // 类型为 [单品多件、单品单件] 时关闭订单占用
          // if (['1', '2'].includes(res.data.outbound.orderTag)) {
          //   commit('SET_ORDER_OCCUPY', false)
          // }
          cb()
        })
      } else if (res.data.status === 1) {
        return 1
      } else if (res.data.status === 3) {
        commit('SET_NONE', true)
        return 3
      }
    }).catch(() => {
      commit('SET_BARCODE', '')
      return 'err'
    })
  },

  // 复核数量blur
  reunite({ commit }, vals) {
    const { val } = vals
    const { barCode, form, sn, cb, topData } = state
    const obj = topData.find(v => barCode.toUpperCase() === v.goodsBarCode.toUpperCase())
    const { wavePickingCode, outboundCode } = form
    if (!obj) return 'err'
    const parms = {
      code: wavePickingCode,
      deliveryCode: obj.deliveryCode,
      goodsBarCode: barCode,
      reviewCount: Number(val),
      deliveryItemCode: obj.code,
      outboundCode: outboundCode,
      action: sn,
      packingMaterialCode: state.lowerData[0].packingMaterialCode
    }
    return update(parms).then(res => {
      if ([0, 1].includes(res.data.status)) {
        commit('SET_CANCEL_ORDER_PROMPT', res.data.outbound.cancelOrderPrompt)
      }
      // commit('SET_COMPLETE', false)
      commit('SET_GREATER', false)
      commit('SET_NONE', false)
      if (res.data.status === 2) {
        commit('SET_GREATER', true)
        // commit('SET_COMPLETE', false)
        commit('SET_NONE', false)
        return false
      } else if (res.data.status === 3) {
        commit('SET_NONE', true)
        // commit('SET_COMPLETE', false)
        commit('SET_GREATER', false)
        return false
      }
      const obj = res.data.goodsList.find(v => barCode.toUpperCase() === v.goodsBarCode.toUpperCase())
      if (!obj) return false
      const goodsName = obj.goodsName
      commit('SET_GOODSNAME', goodsName)
      const { selectData } = state
      if (res.data.status === 0) {
        const obj = topData.find(v => barCode.toUpperCase() === v.goodsBarCode.toUpperCase())
        // 添加商品名称
        const form = {
          wavePickingCode: res.data.outbound.wavePickingCode,
          orderTag: translateStatus(res.data.outbound.orderTag),
          shipmentName: res.data.outbound.shipmentName,
          carrierCode: res.data.outbound.carrierCode,
          carrierName: res.data.outbound.carrierName,
          deliveryCode: obj.deliveryCode,
          shopNick: res.data.deliverieList[0].shopNick,
          deliveryItemCode: res.data.goodsList[0].code,
          outboundCode: res.data.outbound.code,
          code: res.data.deliverieList[0].code
        }
        const arr = [res.data.outbound]
        commit('SET_TIODATA', res.data.goodsList)
        commit('SET_LOWERDATA', res.data.deliverieList.map((a, i) => {
          if (a.packingMaterialName) return { ...a, packingMaterialList: selectData[i].packingMaterialList }
          if (a.code === selectData[i].deliveryCode) {
            return {
              ...a,
              packingMaterialList: selectData[0].packingMaterialList,
              packingMaterialName: selectData[0].packingMaterialList.length > 0 ? selectData[0].packingMaterialList[0].name : '',
              packingMaterialCode: selectData[0].packingMaterialList.length > 0 ? selectData[0].packingMaterialList[0].code : ''
            }
          }
        }))
        commit('SET_FORM', form)
        commit('SET_LETDATA', arr)
        if (res.data.printMap && JSON.stringify(res.data.printMap) !== '{}') {
          const obj = {
            wavePickingCode: res.data.outbound.wavePickingCode,
            orderTag: translateStatus(res.data.outbound.orderTag),
            shipmentName: res.data.outbound.shipmentName,
            carrierCode: res.data.outbound.carrierCode,
            carrierName: res.data.outbound.carrierName,
            deliveryCode: '',
            shopNick: res.data.deliverieList[0].shopNick,
            deliveryItemCode: '',
            outboundCode: res.data.outbound.code,
            code: res.data.deliverieList[0].code
          }
          const arr = [res.data.outbound]
          commit('SET_LETDATA', arr)
          commit('SET_FORM', obj)
          commit('SET_ISDISABLED', false)
          if (res.data.printMap.status === '0') {
            const str = res.data.printMap.data.logisticsCode
            if (str) {
              commit('SET_PRCELTYPE', str)
              commit('SET_PRINTDATA', [res.data.printMap.data])
              commit('SET_FLAG', true)
              return 1
            }
            return 7
          } else {
            commit('SET_MSG', res.data.printMap.message)
            return 9
          }
        }
        cb()
        return true
      } else if (res.data.status === 1 && res.data.printMap && JSON.stringify(res.data.printMap) !== '{}') {
        commit('SET_COMPLETE', true)
        commit('SET_ORDER_OCCUPY', false)
        commit('SET_GREATER', false)
        commit('SET_NONE', false)
        // if () {
        const obj = {
          wavePickingCode: res.data.outbound.wavePickingCode,
          orderTag: translateStatus(res.data.outbound.orderTag),
          shipmentName: res.data.outbound.shipmentName,
          carrierCode: res.data.outbound.carrierCode,
          carrierName: res.data.outbound.carrierName,
          deliveryCode: '',
          code: res.data.deliverieList[0].code,
          shopNick: res.data.deliverieList[0].shopNick,
          deliveryItemCode: '',
          outboundCode: res.data.outbound.code
        }
        const arr = [res.data.outbound]
        commit('SET_LETDATA', arr)
        commit('SET_FORM', obj)
        commit('SET_TIODATA', res.data.goodsList)
        commit('SET_LOWERDATA', res.data.deliverieList.map((a, i) => {
          if (a.packingMaterialName) return { ...a, packingMaterialList: selectData[i].packingMaterialList }
          if (a.code === selectData[i].deliveryCode) {
            return {
              ...a,
              packingMaterialList: selectData[0].packingMaterialList,
              packingMaterialName: selectData[0].packingMaterialList.length > 0 ? selectData[0].packingMaterialList[0].name : '',
              packingMaterialCode: selectData[0].packingMaterialList.length > 0 ? selectData[0].packingMaterialList[0].code : ''
            }
            // return { ...a, packingMaterialList: selectData.packingMaterialList, packingMaterialName: selectData.packingMaterialList[0].name }
          }
        }))
        commit('SET_ISDISABLED', false)
        if (res.data.printMap.status === '0') {
          const str = res.data.printMap.data.logisticsCode
          if (str) {
            commit('SET_PRCELTYPE', str)
            commit('SET_PRINTDATA', [res.data.printMap.data])
            commit('SET_FLAG', true)
            return 1
          }
          return 7
        } else {
          commit('SET_MSG', res.data.printMap.message)
          return 9
        }
      }
    })
  },
  resets({ commit }) {
    commit('SET_FORM', {
      wavePickingCode: '',
      orderTag: '',
      shipmentName: '',
      carrierCode: '',
      deliveryCode: '',
      shopNick: '',
      deliveryItemCode: '',
      outboundCode: ''
    })
    commit('SET_TIODATA', [])
    commit('SET_LOWERDATA', [])
    commit('SET_LETDATA', [])
    commit('SET_COMPLETE', false)
    commit('SET_NONE', false)
    commit('SET_BARCODE', '')
    commit('SET_SN', '')
    commit('SET_NUM', '')
    commit('SET_ISDISABLED', false)
    commit('SET_PRINTDATA', [])
    commit('SET_GREATER', false)
    commit('SET_GOODSNAME', '')
  }
}
function translateStatus(val) {
  if (val === '1') return '单品单件'
  if (val === '2') return '单品多件'
  if (val === '3') return '多品'
  if (val === '4') return '爆品'
  if (val === '5') return '按单'
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
